import { ApiProperty, ApiPropertyOptional } from '@midwayjs/swagger'
import { OmitDto, Rule, RuleType } from '@midwayjs/validate'

import { PageDTO } from '@/dto'

/**
 * 查找用户分页传输对象
 */
export class FindUserPageDTO extends PageDTO implements IFindUserPageDTO {
  @ApiPropertyOptional({ description: '部门ID' })
  @Rule(RuleType.string().empty(''))
  deptId: string

  @ApiPropertyOptional({ description: '账号' })
  @Rule(RuleType.string().empty(''))
  userName: string

  @ApiPropertyOptional({ description: '手机号码' })
  @Rule(RuleType.string().empty(''))
  phone: string

  @ApiPropertyOptional({ description: '状态' })
  @Rule(RuleType.string().empty(''))
  status: string

  @ApiPropertyOptional({ description: '开始时间' })
  @Rule(RuleType.date().empty(''))
  beginDate: Date

  @ApiPropertyOptional({ description: '结束时间' })
  @Rule(RuleType.date().empty(''))
  endDate: Date
}

/**
 *  添加用户传输对象
 */
export class CreateUserDTO implements ICreateUserDTO {
  @ApiProperty({ description: '用户昵称' })
  @Rule(RuleType.string().required().error(new Error('用户昵称不能为空')))
  nickName: string

  @ApiPropertyOptional({ description: '用户昵称' })
  @Rule(RuleType.string().empty(''))
  deptId: string

  @ApiPropertyOptional({ description: '手机号码' })
  @Rule(RuleType.string().empty(''))
  phone: string

  @ApiPropertyOptional({ description: '邮箱' })
  @Rule(RuleType.string().empty(''))
  email: string

  @ApiProperty({ description: '用户账号' })
  @Rule(RuleType.string().required().error(new Error('用户账号不能为空')))
  userName: string

  @ApiProperty({ description: '密码' })
  @Rule(RuleType.string().required().error(new Error('用户密码不能为空')))
  password: string

  @ApiPropertyOptional({ description: '性别' })
  @Rule(RuleType.string().empty(''))
  sex: string

  @ApiPropertyOptional({ description: '状态' })
  @Rule(RuleType.string().empty(''))
  status: string

  @ApiPropertyOptional({ description: '岗位ID列表' })
  @Rule(RuleType.array().items(RuleType.string()).empty())
  postIds: string[]

  @ApiPropertyOptional({ description: '角色ID列表' })
  @Rule(RuleType.array().items(RuleType.string()).empty())
  roleIds: string[]

  @ApiPropertyOptional({ description: '备注' })
  @Rule(RuleType.string().empty(''))
  remark: string
}

/**
 * 修改用户传输对象
 */
export class UpdateUserDTO extends OmitDto(CreateUserDTO, [
  'userName',
  'password'
]) {}

/**
 * 重置密码传输对象
 */
export class ResetPasswordDTO implements IResetPasswordDTO {
  @ApiProperty({ description: '新密码' })
  @Rule(
    RuleType.string()
      .required()
      .error(new Error('密码不能为空'))
      .min(6)
      .error(new Error('密码最少6位'))
  )
  password: string
}
